<?php

namespace App\Http\Controllers\functions;

use Illuminate\Http\Request;
use App\Models\ComUiSet;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class ComUiSetController extends Controller
{
    protected $name = '自定义配置';
    protected $field = ['comid', 'usrid', 'setjson'];

    /**
     * 查询
     */
    public function find(Request $request)
    {
        try {
            $param = json_decode($request->param, true);
            $comid = $this->I($param['comid'] ?? null);
            $usrid = $this->I($param['usrid'] ?? null);
            $where = ' isdel=0';
            if (empty($comid)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|配置编号不能为空!']);
            }
            $where .= " and comid = '$comid' ";
            if (!empty($usrid)) {
                $where .= " and usrid = '$usrid' ";
            }
            //查询redis缓存
            $rediskey = $comid . $usrid;
            $redisdata = $this->Redisgetstring($rediskey);
            if ($redisdata) {
                return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|查询成功!', 'data' => $redisdata, 'msgex' => 'Redis缓存,默认12小时过期']);
            }
            $FSQL = "SELECT id,comid,usrid,setjson 
            FROM ComUiSet where   $where";
            $data = DB::select($FSQL);
            if (!empty($data)) {
                //设置redis缓存
                $this->RedisSetstring($rediskey, $data);
            }
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|查询成功!', 'data' => $data]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|查询失败!','error'=>$e->getMessage()]);
        }
    }

    /**
     * 保存
     */
    public function save(Request $request)
    {
        try {
            $param = json_decode($request->param, true);
            // $id = $this->I($param['id'] ?? null);
            // $isnew = false;
            // if (empty($id)) {
            //     $isnew = true;
            // }
            $list = [];
            if (empty($param)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|保存参数为空!']);
            }
            foreach ($param as $key => $value) {
                if (in_array($key, $this->field)) {
                    $list[$key] = $value;
                }
            }
            if (empty($list)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|没有需要处理的数据!']);
            }
            if (empty($list['comid'])) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|保存失败!配置编号不能为空!']);
            }
            if (empty($list['setjson'])) {
                return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|保存失败!配置列表不能为空!']);
            }
            $FSQL = "select id,comid,usrid,setjson 
            FROM ComUiSet
            where comid='" . $list['comid'] . "' and usrid='" . $list['usrid'] . "' and isdel=0";
            $res = DB::select($FSQL);
            if (empty($res)) {
                $isnew = true;
            } else {
                $isnew = false;
                $id = $res[0]->id;
            }
            //新增
            $where = '';
            if ($isnew) {
                $list['setjson'] = json_encode($list['setjson']);
                // $FSQL = "  select id from ComUiSet where isdel=0 and comid='" . $list['comid'] . "' and usrid='" . $list['usrid'] . "'";
                // if (!empty(DB::select($FSQL))) {
                //     return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '功能编号已存在,不可重复']);
                // }
                //新增
                $res = ComUiSet::create($list);
                if (empty($res)) {
                    return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|新增失败!']);
                }
                $where = " comid='" . $list['comid'] . "' and usrid='" . $list['usrid'] . "' and isdel=0 ";
            } else {
                $where = " id='$id' ";
                //修改   
                $uptab = ComUiSet::find($id);
                $uptab->update($list);
                $res = $uptab;
            }
            $FSQL = "select id,comid,usrid,setjson 
            FROM ComUiSet
            where $where";
            $res = DB::select($FSQL);
            //设置redis缓存
            $rediskey = $list['comid'] . $list['usrid'];
            if (!empty($data)) {
                $this->RedisSetstring($rediskey, $data);
            }
            //记录日志
            $this->log(['type' => '修改', 'billcaption' => '自定义配置', 'des' => '操作人[修改]了自定义配置[' . $list['comid'] . ']']);//,数据为[' . json_encode($list) . ']
            return $this->ajaxReturn(['code' => 1, 'msg' => $this->name . '|保存成功!', 'nowdata' => $res]);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|保存失败!','error'=>$e->getMessage()]);
        }
    }

    /**
     * 删除
     */
    public function delete(Request $request)
    {
        try {
            $param = json_decode($request->param, true);
            $id = $this->I($param['id'] ?? null);
            if (empty($id)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!ID为空']);
            }
            $uptab = ComUiSet::find($id);
            if (empty($uptab)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!功能编号不存在']);
            }
            $uptab->isdel = 1;
            $res = $uptab->save();
            if (empty($res)) {
                return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败!']);
            }
            //记录日志
            $this->log(['type' => '删除', 'billcaption' => '自定义配置', 'des' => '操作人[删除]了自定义配置[' . $id . ']']);
            //设置redis缓存
            $rediskey = $uptab->comid . $uptab->usrid;
            $this->RedisDelstring($rediskey);
            return $this->ajaxReturn(['code' => 1, 'msg' => '删除成功!']);
        } catch (\Exception $e) {
            return $this->ajaxReturn(['code' => 0, 'msg' => $this->name . '|删除失败!','error'=>$e->getMessage()]);
        }
    }

}
